package com.ant.storage.utils;

import cn.hutool.core.date.DateUtil;
import org.apache.http.ParseException;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.*;

/**
 * @author hzd
 * @version 1.0
 * @date 2022/3/29 11:49
 */
public class DateFormat {

    // public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat date_sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 当前日历，这里用中国时间表示
     *
     * @return 以当地时区表示的系统当前日历
     */
    public static Calendar getCalendar() {
        return Calendar.getInstance();
    }

    /**
     * 系统时间的秒数
     *
     * @return 系统时间的秒数
     */
    public static long getSeconds() {
        return Instant.now().getEpochSecond();
    }

    /**
     * 指定毫秒数表示日期的默认显示，具体格式：年-月-日
     *
     * @param millis 指定的毫秒数
     * @return 指定毫秒数表示日期按“年-月-日“格式显示
     */
    public static String formatDate(long millis) {
        return date_sdf.format(new Date(millis));
    }

    /**
     * @param startDateStr 开始时间 年-月-日 时:分:秒
     * @param endDateStr   结束时间 年-月-日 时:分:秒
     * @Description: 获取两个时间段 之间的所有日期，具体格式：年-月-日
     * @Author: eros
     * @Date: 2020/4/15 10:32
     * @Return: java.util.List<java.lang.String>
     * @Exception:
     */
    public static List<String> getDateList(String startDateStr, String endDateStr) throws ParseException,
            java.text.ParseException {
        List<String> result = new ArrayList<>();
        // 将 str时间转为 Date 类型
        Date startDate = datetimeFormat.parse(startDateStr);
        Date endDate = datetimeFormat.parse(endDateStr);
        // 这里主要借助于Calendar 对于两个时间的比较，来存储
        Calendar startCal = getCalendar();
        startCal.setTime(startDate);
        Calendar endCal = getCalendar();
        endCal.setTime(endDate);
        do {
            result.add(formatDate(startCal.getTimeInMillis()));
            startCal.add(Calendar.DAY_OF_YEAR, 1);
        } while (startCal.before(endCal));
        // 因为精细度到 日期，所有最后一个日期难以处理，需要单独来做
        String end = formatDate(endCal.getTimeInMillis());
        if (!result.contains(end)) {
            result.add(end);
        }
        return result;
    }


    public static boolean getDateFormat(Date str, Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str1 = format.format(str);
        String str2 = format.format(date);
        Date date1;
        Date date2;
        try {
            date1 = format.parse(str1);//截时间
            date2 = format.parse(str2);
            if (date1.getTime() < date2.getTime()) {
                return true;
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return false;

    }


    /**
     * 获取时间格式
     *
     * @param str 格式 yyyyMMdd
     * @return
     */
    public static String getDateFormat(String str) {
        Date curDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        format = new SimpleDateFormat(str);
        String DateToStr = format.format(curDate);
        DateToStr = format.format(curDate);
        return DateToStr;
    }

    public static String getCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        return df.format(System.currentTimeMillis());
    }

    public static String getDepCurrentDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(System.currentTimeMillis());
    }

    public static Date getCurrentDate(String str) throws java.text.ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Date date = fmt.parse(str);
        return date;
    }


    public static String getCurrentDates(String str) throws java.text.ParseException {
        if (str == null || str.equals("")) {
            return null;
        } else {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            Date date = fmt.parse(str);
            String format = fmt.format(date);
            long timestamp = date.getTime() / 1000L;
            return String.valueOf(timestamp);
        }

    }


    /**
     * 获取本月的第一天和最后一天
     *
     * @return
     */
    public static ArrayList firstDayAndLastDay() {
        ArrayList list = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        //获得本月第一天
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String firstDay = sdf.format(calendar.getTime());

        list.add(firstDay);
        //获得本月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDay = sdf.format(calendar.getTime());

        list.add(lastDay);
        return list;
    }


    public static LocalDate DateLocalDate(Date date) {
        //   Date date = new Date();
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        //Date转换为LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        //获取LocalDate
        LocalDate localDate = localDateTime.toLocalDate();

        return localDate;
    }
    //        Map<String, Object> map = new HashMap<>();
    //        List<GoodsTest> goodsList = new ArrayList<>();
    //        goodsList.add(new GoodsTest("袜子", 30, BigDecimal.valueOf(20)));
    //
    //
    //        goodsList.stream().forEach(g -> g.setTotal(g.getPrice().multiply(BigDecimal.valueOf(g.getNum()))));
    //        System.out.println(goodsList);
    //        int buySum = goodsList.stream().map(item -> Integer.valueOf(item.getNum())).mapToInt(Integer::intValue)
    //        .sum();
    //        System.out.println("collect=>" + buySum);
    //        double v =
    //                goodsList.stream().map(a -> new BigDecimal(String.valueOf(a.getNum())).multiply(new BigDecimal
    //                (String.valueOf(a.getPrice())))).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue();
    //        System.out.println("v=>" + v);
    //        System.out.println("goodsList=>" + goodsList);
    //        map.put("goodsList", goodsList);
    //        map.put("total", goodsList.stream().map(GoodsTest::getTotal).reduce(BigDecimal.ZERO, BigDecimal::add));
    //        System.out.println(map);
    //        BigDecimal divide = BigDecimal.valueOf(v).divide(BigDecimal.valueOf(buySum), 3, BigDecimal
    //        .ROUND_HALF_DOWN);
    //        System.out.println(divide);

    public static Date addOneMonth(String date, Integer moth) {
        try {
            System.out.println("----->" + date);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sdf.parse(date));
            calendar.add(Calendar.MONTH, moth);
            return calendar.getTime();
        } catch (ParseException | java.text.ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getCurrentDates() {
        Date date = new Date();
        return DateUtil.formatDate(date);
    }

    public static String monthOne(String time) {
        Date date = DateFormat.addOneMonth(time, 1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义新的日期格式
        String str = formatter.format(date);
        String substring = str.substring(0, 8);
        return substring + "01";
    }

    public static String simpleDateFormat(Date time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(time);
    }

    public static String simpleDateFormats(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static List<Integer> getYearsBetween(int startYear, int endYear) {
        List<Integer> years = new ArrayList<>();
        Year currentYear = Year.of(startYear);
        while (!currentYear.isAfter(Year.of(endYear))) {
            years.add(currentYear.getValue());
            currentYear = currentYear.plusYears(1);
        }
        return years;
    }

}
